﻿using Niknak.Services.TaskScheduler;
using Niknak.Services.TaskScheduler.Processors;
using Sandbox.Data.Mongo;
using System;
using System.Diagnostics;
using System.Threading;

namespace Sandbox.Business.Processors
{
    public class TestProc2 : BaseProcessor<AdvancedTask<ExceptionHandler, ScheduledTask, AuditLog, AuditLogEntry>, AdvancedTaskConfig<ScheduledTask>, AuditLog, AuditLogEntry>
    {
        [StartArgument(Prompt="String argument", IsRequired=true, HelpText="This is some help text"/*, DefaultValue="dummy value"*/)]
        private string StringArgument;

        public override void Processor_Load(string startArguments)
        {
            Console.WriteLine("StringArgument = " + StringArgument);
        }
        
        public override Niknak.Services.TaskScheduler.Enums.TaskOutcome ProcessTask(AdvancedTaskConfig<ScheduledTask> config, AuditLog currentLog, ref bool terminate)
        {
            base.CurrentLog = currentLog;

            try
            {
                DateTime currentUtcTime = UtcNow;

                base.CurrentLog.IterationDate = currentUtcTime;
                if (base.CurrentLog.EnableLogging)
                    base.CurrentLog.Save();

                while (!terminate)
                    Thread.Sleep(1000);

                return Niknak.Services.TaskScheduler.Enums.TaskOutcome.OK;
            }
            catch (Exception ex)
            {
                new ExceptionHandler().ExceptionLogEntry(ex);
                base.CurrentLog.AddEntry(Niknak.Services.TaskScheduler.Enums.AuditLogEntryType.Error, "An exception occured and the operation was aborted; " + ex.Message);
                return Niknak.Services.TaskScheduler.Enums.TaskOutcome.Aborted;
            }
        }
    }
}
